package com.ymm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ymm.domain.entity.Menu;
import com.ymm.domain.vo.AllMenuVo;
import com.ymm.domain.vo.MenuListVo;
import com.ymm.domain.vo.PageVo;
import com.ymm.domain.vo.ResponseResult;
import com.ymm.mapper.MenuMapper;
import com.ymm.service.MenuService;
import com.ymm.service.RoleMenuService;
import com.ymm.utils.BeanCopyUtils;
import org.apache.xmlbeans.impl.xb.xsdschema.All;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单权限表(Menu)表服务实现类
 *
 * @author 椰咩咩咩
 * @since 2023-03-31 00:29:33
 */
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Resource
    private RoleMenuService roleMenuService;

    @Override
    public ResponseResult roleNoPerm(Long id) {
        List<Long> mids = roleMenuService.getRoleHasMenu(id);
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.notIn(mids.size() > 0, Menu::getId, mids);
        List<Menu> menus = list(wrapper);
        List<AllMenuVo> menuVos = BeanCopyUtils.copyBeanList(menus, AllMenuVo.class);
        return ResponseResult.okResult(menuVos);
    }

    @Override
    public ResponseResult getMenusByRole(Long id) {
        List<Long> mids = roleMenuService.getRoleHasMenu(id);
        if (mids.size() <= 0) {
            return ResponseResult.okResult();
        }
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Menu::getId, mids);
        List<Menu> menus = list(wrapper);
        return ResponseResult.okResult(menus);
    }

    @Override
    public ResponseResult pageMenus(Integer pageNum, Integer pageSize, String label) {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(label), Menu::getLabel, label);
        Page<Menu> page = new Page<>(pageNum, pageSize);
        page(page, wrapper);
        List<Menu> menus = page.getRecords();
        List<MenuListVo> menuListVos = BeanCopyUtils.copyBeanList(menus, MenuListVo.class);
        return ResponseResult.okResult(new PageVo(menuListVos, page.getTotal()));
    }
}

